package com.have.blog.admin.controller;


import com.have.blog.admin.annotation.operationLogger.OperationLogger;
import com.have.blog.admin.domain.dto.UserDto;
import com.have.blog.admin.service.BlogUserService;
import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.Delete;
import com.have.blog.base.validator.group.GetList;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.validator.group.Update;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户表 RestApi
 */
@RestController
@Api(value = "用户相关接口", tags = {"用户相关接口"})
@RequestMapping("/user")
@Slf4j
public class BlogUserController {

    @Autowired
    private BlogUserService userService;

    @ApiOperation(value = "获取用户列表", notes = "获取用户列表", response = String.class)
    @PostMapping("/getList")
    public Wrapper<CommonPage> getList(@Validated({GetList.class}) @RequestBody UserDto userDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取用户列表: {}", userDto);
        List<UserDto> list = userService.getList(userDto);
        return Wrapper.success(CommonPage.restPage(list));
    }

    @OperationLogger(value = "新增用户")
    @ApiOperation(value = "新增用户", notes = "新增用户", response = String.class)
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class}) @RequestBody UserDto userDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("新增用户: {}", userDto);
        return userService.addUser(userDto);
    }

    @OperationLogger(value = "编辑用户")
    @ApiOperation(value = "编辑用户", notes = "编辑用户", response = String.class)
    @PostMapping("/edit")
    public Wrapper edit(@Validated({Update.class}) @RequestBody UserDto userDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("编辑用户: {}", userDto);
        return userService.editUser(userDto);
    }

    @OperationLogger(value = "删除用户")
    @ApiOperation(value = "删除用户", notes = "删除用户", response = String.class)
    @PostMapping("/delete")
    public Wrapper delete(@Validated({Delete.class}) @RequestBody UserDto userDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("删除用户: {}", userDto);
        return userService.deleteUser(userDto);
    }

    @OperationLogger(value = "重置用户密码")
    @ApiOperation(value = "重置用户密码", notes = "重置用户密码", response = String.class)
    @PostMapping("/resetUserPassword")
    public Wrapper resetUserPassword(@Validated({Delete.class}) @RequestBody UserDto userDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("重置用户密码: {}", userDto);
        return userService.resetUserPassword(userDto);
    }
}
